home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / gs3.53 / gs_pdf.ps < prev    next >
Text File  |  1996-01-10  |  12KB  |  414 lines

  1. %    Copyright (C) 1994, 1995 Aladdin Enterprises.  All rights reserved.
  2.  
  3. % gs_pdf.ps
  4. % ProcSet for PostScript files created by the PDF to PostScript converter.
  5.  
  6. % This ProcSet requires only a Level 1 interpreter except for:
  7. %    - The general case of image and imagemask;
  8. %    - The color space and general color setting operators.
  9.  
  10. mark                % patches
  11. /currentglobal false
  12. /setglobal { pop } bind
  13. /packedarray { array astore readonly } bind
  14. /setcmykcolor
  15.  { 1 exch sub
  16.    4 -1 roll 1 exch sub 1 index mul
  17.    4 -1 roll 1 exch sub 2 index mul
  18.    4 -2 roll exch 1 exch sub mul
  19.    setrgbcolor
  20.  } bind
  21. /.dicttomark
  22.  { counttomark 2 idiv dup dict begin { def } repeat pop currentdict end
  23.  } bind
  24. /.knownget { 2 copy known { get true } { pop pop false } ifelse } bind
  25. counttomark 2 idiv { 1 index where { pop pop pop } { def } ifelse } repeat pop
  26.  
  27. currentglobal true setglobal
  28.  
  29. % Define pdfmark.  Don't allow it to be bound in.
  30. % Also don't define it in systemdict, because this leads some Adobe code
  31. % to think this interpreter is a distiller.
  32. userdict /pdfmark { cleartomark } bind put
  33.  
  34. % This ProcSet is designed so that it can be used either to execute PDF
  35. % (the default) or to convert PDF to PostScript.  See ! and ~ below.
  36.  
  37. userdict /GS_PDF_ProcSet 119 dict dup begin
  38.  
  39. % ---------------- Abbreviations ---------------- %
  40.  
  41. /bdef { bind def } bind def
  42.  
  43. % ---------------- Operator execution ---------------- %
  44.  
  45. % We record "operator" names in a dictionary with their argument counts,
  46. % so that they can easily be redefined later to write PostScript in
  47. % addition to (or instead of) being executed.
  48.  
  49. /numargsdict 100 dict def
  50. /!        % <procname> <proc> <numargs> ! -
  51.  { //numargsdict 3 index 3 -1 roll put def
  52.  } bdef
  53. /~        % <procname> <opname> <numargs> ~ -
  54.  { exch cvx 1 packedarray cvx exch !
  55.  } bdef
  56.  
  57. % ---------------- Graphics state stack ---------------- %
  58.  
  59. % PDF adds a number of parameters to the graphics state.
  60. % We implement this by pushing and popping a dictionary
  61. % each time we do a PDF gsave or grestore.
  62. % The keys in this dictionary are as follows:
  63. %    self            % identifies the dictionary as one of ours
  64. %    Show
  65. %    TextOrigin        % origin of current line, in text space
  66. %    TextSaveMatrix        % matrix at time of BT
  67. % (The following correspond directly to PDF state parameters.)
  68. %    FillColor
  69. %    FillColorSpace
  70. %    StrokeColor
  71. %    StrokeColorSpace
  72. %    TextSpacing
  73. %    TextHScaling
  74. %    Leading
  75. %    TextFont
  76. %    TextMatrix
  77. %    TextRise
  78. %    TextRenderingMode
  79. %    WordSpacing
  80.  
  81. /nodict 1 dict def
  82. nodict /self { //nodict } executeonly put
  83. nodict readonly pop
  84.  
  85. /beginpage { //nodict begin graphicsbeginpage textbeginpage } bdef
  86. /endpage { showpage end } bdef
  87.  
  88. /graphicsbeginpage { initgraphics  0 g  0 G } bdef
  89.  
  90. /gput        % <value> <key> gput -
  91.  { currentdict //nodict eq { /self dup load end 3 dict begin def } if
  92.    exch def
  93.  } bdef
  94.  
  95. /q_
  96.  { gsave //nodict begin
  97.  } bdef
  98. /q /q_ load 0 !
  99. % Some PDF files have excess Q operators!
  100. /Q_
  101.  { currentdict /self .knownget { exec //nodict eq { end grestore } if } if
  102.  } bdef
  103. /Q /Q_ load 0 !
  104.  
  105. % ---------------- Graphics state parameters ---------------- %
  106.  
  107. /d /setdash 2 ~
  108. /i /setflat 1 ~
  109. /j /setlinejoin 1 ~
  110. /J /setlinecap 1 ~
  111. /M /setmiterlimit 1 ~
  112. /w /setlinewidth 1 ~
  113.  
  114. % ---------------- Color setting ---------------- %
  115.  
  116. /fcput        % <color> <colorspace> fcput -
  117.  { /FillColorSpace gput /FillColor gput
  118.  } bdef
  119. /scput        % <color> <colorspace> scput -
  120.  { /StrokeColorSpace gput /StrokeColor gput
  121.  } bdef
  122.  
  123. /CSdict 4 dict dup begin
  124.   /setcolorrendering where
  125.    { pop
  126.      /CalGray
  127.       { 1 get dup /Gamma .knownget
  128.      { dup length 1 add dict copy
  129.        dup /DecodeA 4 -1 roll /exp load 2 packedarray cvx put
  130.          }
  131.     if /CIEBasedA exch 2 array astore setcolorspace
  132.       } bdef
  133.      /CalRGB
  134.       { 1 get dup /Gamma known 1 index /Matrix known or
  135.      { dup length 2 add dict copy
  136.        dup /Matrix .knownget { 1 index /MatrixABC 3 -1 roll put } if
  137.        dup /Gamma .knownget
  138.         { [ exch { /exp load 2 packedarray cvx } forall
  139.           ] 1 index /DecodeABC 3 -1 roll put
  140.         }
  141.        if
  142.          }
  143.     if /CIEBasedABC exch 2 array astore setcolorspace
  144.       } bdef
  145.      /CalCMYK { pop 0 0 0 1 setcmykcolor } bdef    % not supported yet
  146.    }
  147.    { /CalGray { pop 0 setgray } bdef
  148.      /CalRGB { pop 0 0 0 setrgbcolor } bdef
  149.      /CalCMYK { pop 0 0 0 1 setcmykcolor } bdef
  150.    }
  151.   ifelse
  152. end def
  153. /csset            % <cspace> csset <color> <cspace>
  154.  { dup 0 get //CSdict exch .knownget { exec } { setcolorspace } ifelse
  155.    mark currentcolor counttomark 1 eq { exch pop } { ] } ifelse
  156.    currentcolorspace
  157.  } bdef
  158.  
  159. /csdevgray [/DeviceGray] readonly def
  160. /csdevrgb [/DeviceRGB] readonly def
  161. /csdevcmyk [/DeviceCMYK] readonly def
  162.  
  163. /g { //csdevgray fcput } 1 !
  164. /G { //csdevgray scput } 1 !
  165. /rg { 3 array astore //csdevrgb fcput } 3 !
  166. /RG { 3 array astore //csdevrgb scput } 3 !
  167. /k { 4 array astore //csdevcmyk fcput } 4 !
  168. /K { 4 array astore //csdevcmyk scput } 4 !
  169. /cs { csset fcput } 1 !
  170. /CS { csset scput } 1 !
  171. % We have to break up sc according to the number of operands.
  172. /sc1 { /FillColor gput } 1 !
  173. /SC1 { /StrokeColor gput } 1 !
  174. /sc3 { FillColor astore pop } 3 !
  175. /SC3 { StrokeColor astore pop } 3 !
  176. /sc4 { FillColor astore pop } 4 !
  177. /SC4 { StrokeColor astore pop } 4 !
  178.  
  179. % ---------------- Color installation ---------------- %
  180.  
  181. % Establish a given color (and color space) as current.
  182. /setfillcolor { FillColor FillColorSpace setgcolor } def
  183. /setstrokecolor { StrokeColor StrokeColorSpace setgcolor } def
  184. /setgcolor    % (null | <color>) <colorspace> setgcolor -
  185.  { 1 index null eq
  186.     { pop pop }
  187.     { setcolorspace dup type /arraytype eq { aload pop } if setcolor }
  188.    ifelse
  189.  } bdef
  190. /fsexec        % <fillop|strokeop> fsexec -
  191.  {        % Preserve the current point, if any.
  192.     { currentpoint } stopped
  193.     { cvx exec }
  194.     { 3 -1 roll cvx exec moveto }
  195.    ifelse
  196.  } bdef
  197.  
  198. % ---------------- Transformations ---------------- %
  199.  
  200. /cmmatrix matrix def
  201. /cm { //cmmatrix astore concat } 6 !
  202.  
  203. % ---------------- Path creation ---------------- %
  204.  
  205. /m /moveto 2 ~
  206. /l /lineto 2 ~
  207. /c /curveto 6 ~
  208. /h /closepath 0 ~
  209. /v { currentpoint 6 2 roll curveto } 4 !
  210. /y { 2 copy curveto } 4 !
  211. /re
  212.  { 4 2 roll moveto  exch dup 0 rlineto  0 3 -1 roll rlineto  neg 0 rlineto
  213.    closepath
  214.  } 4 !
  215.  
  216. % ---------------- Path painting and clipping ---------------- %
  217.  
  218. /S_ { setstrokecolor /stroke fsexec } bdef
  219. /S { S_ } 0 !
  220. /f { setfillcolor /fill fsexec } 0 !
  221. /f* { setfillcolor /eofill fsexec } 0 !
  222. /n_ { newpath } bdef        % don't allow n_ to get bound in
  223. /n { n_ } 0 !
  224. /s { closepath S_ } 0 !
  225. /B_ { gsave setfillcolor fill grestore S_ } bdef
  226. /B /B_ load 0 !
  227. /b { closepath B_ } 0 !
  228. /B*_ { gsave setfillcolor eofill grestore S_ } bdef
  229. /B* /B*_ load 0 !
  230. /b* { closepath B*_ } 0 !
  231.  
  232. % Clipping:
  233.  
  234. /Wdict 4 dict dup begin
  235. /S_ { gsave setstrokecolor stroke grestore n_ } bdef
  236. /f { gsave setfillcolor fill grestore n_ } 0 !
  237. /f* { gsave setfillcolor eofill grestore n_ } 0 !
  238. /n_ { end clip newpath } bdef
  239. end readonly def
  240. /W { //Wdict begin } 0 !
  241. /W*dict 4 dict dup begin
  242. /S_ { gsave setstrokecolor stroke grestore n_ } bdef
  243. /f { gsave setfillcolor fill grestore n_ } 0 !
  244. /f* { gsave setfillcolor eofill grestore n_ } 0 !
  245. /n_ { end eoclip newpath } bdef
  246. end readonly def
  247. /W* { //W*dict begin } 0 !
  248.  
  249. % ---------------- Images ---------------- %
  250.  
  251. % We mustn't bind these now, since they are Level 2 operators.
  252. /Is        % <imagedict> Is <imagedict> <datasource>
  253.  { dup /DataSource get string
  254.    currentfile /ASCII85Decode filter /RunLengthDecode filter exch
  255.     { readstring pop }
  256.    aload length 2 add packedarray cvx
  257.  } bdef
  258. /EI { } def    % placeholder, only needed when writing PostScript
  259. % Note that ID takes a dictionary, not separate values;
  260. % that DataSource is the size of the row buffer in bytes;
  261. % and that ImageMask is required, not optional.
  262. /ID    % <imagedict> ID -
  263.  { Is 1 index /DataSource 3 -1 roll put
  264.    dup /ImageMask get
  265.     { /imagemask }
  266.     { dup /ColorSpace get setcolorspace /image }
  267.    ifelse cvx exec EI
  268.  } 1 !
  269.  
  270. % ---------------- Text control ---------------- %
  271.  
  272. /textbeginpage
  273.  { /TextSpacing 0 def        % 0 Tc
  274.    /TextLeading 0 def        % 0 TL
  275.    /TextRenderingMode 0 def    % 0 Tr
  276.    /TextRise 0 def        % 0 Ts
  277.    /WordSpacing 0 def        % 0 Tw
  278.    /TextHScaling 1.0 def    % 100 Tz
  279.    /TextFont null def
  280.    /Show { showfirst } def
  281.  } bdef
  282.  
  283. % Contrary to the statement in the PDF manual, BT and ET *can* be nested,
  284. % if the CharProc for a Type 3 font does a BT/ET itself.
  285. % Since we always call the CharProc inside a q_/Q_, we simply ensure that
  286. % the text state is saved and restore like the rest of the extended
  287. % graphics state.
  288.  
  289. /settextmatrix
  290.  { TextMatrix concat
  291.    TextHScaling 1 ne { TextHScaling 1 scale } if
  292.    TextRise 0 ne { 0 TextRise translate } if
  293.  } bdef
  294. /settextstate { TextSaveMatrix setmatrix settextmatrix } bdef
  295.  
  296. /BT
  297.  { currentdict /TextMatrix .knownget
  298.     { identmatrix pop }
  299.     { matrix /TextMatrix gput }
  300.    ifelse
  301.    currentdict /TextOrigin .knownget
  302.     { dup 0 0 put 1 0 put }
  303.     { [0 0] cvx /TextOrigin gput }
  304.    ifelse
  305.    /Show { showfirst } def
  306.    currentdict /TextSaveMatrix .knownget not
  307.     { matrix dup /TextSaveMatrix gput }
  308.    if currentmatrix pop settextmatrix 0 0 moveto
  309.    TextFont dup null eq { pop } { setfont } ifelse
  310.  } bind 0 !
  311. /ET
  312.  { TextSaveMatrix setmatrix
  313.  } bind 0 !
  314. /Tc_ { /TextSpacing gput /Show { showfirst } def } bdef
  315. /Tc { Tc_ } 1 !
  316. /TL { /TextLeading gput } bind 1 !
  317. /Tr { /TextRenderingMode gput /Show { showfirst } def } bind 1 !
  318. /Ts { /TextRise gput settextstate } bind 1 !
  319. /Tw_ { /WordSpacing gput /Show { showfirst } def } bdef
  320. /Tw { Tw_ } 1 !
  321. /Tz { 100 div /TextHScaling gput settextstate } bind 1 !
  322.  
  323. /Tf        % <font> <scale> Tf -
  324.  { dup 1 eq { pop } { scalefont } ifelse
  325.    dup setfont /TextFont gput
  326.  } 2 !
  327.  
  328. % ---------------- Text positioning ---------------- %
  329.  
  330. /Td_
  331.  { TextOrigin exch 4 -1 roll add 3 1 roll add
  332.    2 copy /TextOrigin load astore pop moveto
  333.  } bdef
  334. /Td { Td_ } 2 !
  335. /TD { dup neg /TextLeading gput Td_ } 2 !
  336. /T*_ { 0 TextLeading neg Td_ } bdef
  337. /T* { T*_ } 0 !
  338. /Tm
  339.  { TextMatrix astore pop settextstate
  340.    0 0 /TextOrigin load astore pop
  341.    0 0 moveto
  342.  } 6 !
  343.  
  344. % ---------------- Text painting ---------------- %
  345.  
  346. /textrenderingprocs [        % (0 is handled specially)
  347.    { tf } { tS } { tB } { tn }
  348.     % We don't know what the clipping modes mean....
  349.    4 copy
  350. ] readonly def
  351. /setshowstate
  352.  { /Show WordSpacing 0 eq TextSpacing 0 eq and
  353.     { TextRenderingMode 0 eq
  354.        { { setfillcolor show } }
  355.        { { false charpath textrenderingprocs TextRenderingMode get exec } }
  356.       ifelse
  357.     }
  358.     { TextRenderingMode 0 eq
  359.        { WordSpacing 0 eq
  360.           { { setfillcolor TextSpacing exch 0 exch ashow } }
  361.       { TextSpacing 0 eq
  362.          { { setfillcolor WordSpacing exch 0 exch 32 exch widthshow } }
  363.          { { setfillcolor WordSpacing exch TextSpacing exch 0 32 4 2 roll 0 exch awidthshow } }
  364.         ifelse
  365.       }
  366.      ifelse
  367.        }
  368.        { { WordSpacing TextSpacing 2 index
  369.             % Implement the combination of t3 and false charpath.
  370.             % Stack: xword xchar string
  371.        0 1 2 index length 1 sub
  372.         { 2 copy 1 getinterval false charpath
  373.             % Stack: xword xchar string i
  374.           4 copy get 32 eq { add } { exch pop } ifelse 0 rmoveto
  375.           pop
  376.         }
  377.        for pop pop pop pop
  378.        textrenderingprocs TextRenderingMode get exec
  379.      }
  380.        }
  381.       ifelse
  382.     }
  383.    ifelse def
  384.  } bdef
  385. /showfirst { setshowstate Show } def
  386.  
  387. /Tj { Show } 1 !
  388. /' { T*_ Show } 1 !
  389. /" { exch Tc_ exch Tw_ T*_ Show } 3 !
  390. /TJ
  391.  {  { dup type /stringtype eq
  392.        { Show }
  393.        { neg 1000 div 0 rmoveto }    % probably wrong
  394.       ifelse
  395.     }
  396.    forall
  397.  } 1 !
  398.  
  399. /tf { setfillcolor currentpoint fill moveto } bdef
  400. /tn { currentpoint newpath moveto } bdef
  401. % For stroking characters, temporarily restore the graphics CTM so that
  402. % the line width will be transformed properly.
  403. /Tmatrix matrix def
  404. /tS
  405.  { setstrokecolor
  406.    currentpoint //Tmatrix currentmatrix TextSaveMatrix setmatrix stroke
  407.    setmatrix moveto
  408.  } bdef
  409. /tB { gsave tf grestore tS } bdef
  410.  
  411. end readonly put        % GS_PDF_ProcSet
  412.  
  413. setglobal
  414.